﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalManagementSystem.Utils;
using MedicalManagementSystem.Models;
using MedicalManagementSystem.Helpers;

namespace MedicalManagementSystem.Controllers
{
    public class HospitalController : GenericController
    {
        //
        // GET: /Hospital/

        public ActionResult Index()
        {
            Menu.ID = "adm,hospital";
            return View(DbContext.Instance.Hospital);
        }

        //
        // GET: /Hospital/Details/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole | UserRoles.AdminRole)]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Hospital/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole | UserRoles.AdminRole)]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Hospital/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole | UserRoles.AdminRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "id")]Hospital hospital)
        {
            try
            {
                DbContext.Instance.AddToHospital(hospital);
                DbContext.Instance.SaveChanges();

                return RedirectToAction("Index");
            }
            catch (RuleViolationException ruleException)
            {
                AddViolataionsToModelState(ruleException);
            }

            return View(hospital);
        }
        
        //
        // GET: /Hospital/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole | UserRoles.AdminRole)]
        public ActionResult Edit(int id)
        {
            var hospital = Hospital.GetByPK(id);
            return View(hospital);
        }

        //
        // POST: /Hospital/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole | UserRoles.AdminRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Hospital hospital)
        {
            try
            {
                var original = Hospital.GetByPK(id);
                DbContext.Instance.ApplyPropertyChanges(original.EntityKey.EntitySetName, hospital);
                DbContext.Instance.SaveChanges();

                return RedirectToAction("Index");

            }
            catch (RuleViolationException ruleException)
            {
                AddViolataionsToModelState(ruleException);
            }
            return View(hospital);
        }

        [EnumAuthorize(Roles = UserRoles.RegistratorRole | UserRoles.AdminRole)]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(int id)
        {
            try
            {
                var hospital = Hospital.GetByPK(id);
                DbContext.Instance.DeleteObject(hospital);
                DbContext.Instance.SaveChanges(true);
            }
            catch (Exception e)
            {

            }

            return RedirectToAction("Index");
        }
    }
}
